#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load(
    "//zetasql/compliance:builddefs.bzl",
    "zetasql_compliance_test",
)

package(default_visibility = ["//zetasql/base:zetasql_implementation"])

cc_library(
    name = "type_helpers",
    srcs = ["type_helpers.cc"],
    hdrs = ["type_helpers.h"],
    deps = [
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:catalog",
        "//zetasql/public:strings",
        "//zetasql/public:type",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "type_helpers_test",
    size = "small",
    srcs = ["type_helpers_test.cc"],
    deps = [
        ":type_helpers",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:id_string",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/testing:test_value",
    ],
)

# Tested by value_expr_test.
cc_library(
    name = "proto_util",
    srcs = ["proto_util.cc"],
    hdrs = ["proto_util.h"],
    deps = [
        "//zetasql/base",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:internal_value",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/functions:arithmetics",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/proto:type_annotation_cc_proto",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_protobuf//:protobuf",
        "@com_google_protobuf//:protobuf_lite",
    ],
)

cc_library(
    name = "statement_evaluator",
    srcs = ["statement_evaluator.cc"],
    hdrs = ["statement_evaluator.h"],
    deps = [
        ":evaluator_table_iterator_cc_proto",
        ":type_helpers",
        ":type_parameter_constraints",
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:status_payload_utils",
        "//zetasql/parser",
        "//zetasql/proto:script_exception_cc_proto",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:catalog",
        "//zetasql/public:error_helpers",
        "//zetasql/public:evaluator",
        "//zetasql/public:evaluator_table_iterator",
        "//zetasql/public:id_string",
        "//zetasql/public:multi_catalog",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:parse_location",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:simple_catalog_util",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/scripting:error_helpers",
        "//zetasql/scripting:parsed_script",
        "//zetasql/scripting:script_executor",
        "//zetasql/scripting:script_segment",
        "//zetasql/scripting:type_aliases",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "common",
    srcs = [
        "common.cc",
        "variable_id.cc",
    ],
    hdrs = [
        "common.h",
        "variable_id.h",
    ],
    deps = [
        "//zetasql/base",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:collator_lite",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:serialization_cc_proto",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "evaluation",
    srcs = [
        "aggregate_op.cc",
        "analytic_op.cc",
        "evaluation.cc",
        "function.cc",
        "operator.cc",
        "pattern_matching_op.cc",
        "relational_op.cc",
        "tuple.cc",
        "tuple_comparator.cc",
        "value_expr.cc",
    ],
    hdrs = [
        "evaluation.h",
        "function.h",
        "operator.h",
        "tuple.h",
        "tuple_comparator.h",
    ],
    deps = [
        ":common",
        ":proto_util",
        ":type_parameter_constraints",
        ":variable_generator",
        "//zetasql/base",
        "@com_google_protobuf//:protobuf",
        "@com_google_googleapis//google/type:date_cc_proto",
        "@com_google_googleapis//google/type:timeofday_cc_proto",
        # buildcleaner: keep
        "//zetasql/common:string_util",
        "//zetasql/common:thread_stack",
        "//zetasql/public/functions:array_zip_mode_cc_proto",
        "//zetasql/public/functions:bitwise_agg_mode_cc_proto",
        "//zetasql/public/functions:differential_privacy_cc_proto",
        "//zetasql/public/functions:rank_type_cc_proto",
        "//zetasql/public/functions:rounding_mode_cc_proto",
        "//zetasql/public/types:timestamp_util",
        "//zetasql/public:anonymization_utils",
        "//zetasql/common:errors",
        "//zetasql/common:initialize_required_fields",
        "//zetasql/common:internal_value",
        "//zetasql/proto:anon_output_with_report_cc_proto",
        "//zetasql/public/types",
        "//zetasql/public:builtin_function_cc_proto",
        "//zetasql/public:catalog",
        "//zetasql/public:civil_time",
        "//zetasql/public:coercer",
        "//zetasql/public:collator_lite",
        "//zetasql/public:evaluator_table_iterator",
        "//zetasql/public:function",
        "//zetasql/public:interval_value",
        "//zetasql/public:json_value",
        "//zetasql/public:language_options",
        "//zetasql/public:numeric_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:pico_time",
        "//zetasql/public:proto_value_conversion",
        "//zetasql/public:sql_tvf",
        "//zetasql/public:templated_sql_tvf",
        "//zetasql/public:timestamp_picos_value",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:uuid_value",
        "//zetasql/public:value",
        "//zetasql/public/functions:arithmetics",
        "//zetasql/public/functions:bitcast",
        "//zetasql/public/functions:bitwise",
        "//zetasql/public/functions:common_proto",
        "//zetasql/public/functions:numeric",
        "//zetasql/public/functions:comparison",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/functions:datetime_cc_proto",
        "//zetasql/public/functions:distance",
        "//zetasql/public/functions:string_format",
        "//zetasql/public/functions:generate_array",
        "//zetasql/public/functions:json",
        "//zetasql/public/functions:math",
        "//zetasql/public/functions:net",
        "//zetasql/public/functions:normalize_mode_cc_proto",
        "//zetasql/public/functions:parse_date_time",
        "//zetasql/public/functions:percentile",
        "//zetasql/public/functions:regexp",
        "//zetasql/public/functions:string",
        "//zetasql/public/functions:like",
        "//zetasql/public/functions/match_recognize:compiled_pattern",
        "//zetasql/public/functions/match_recognize:match_partition",
        "//zetasql/public/proto:type_annotation_cc_proto",
        "//zetasql/reference_impl/functions:like",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/base:strings",
        "@com_google_googletest//:gtest",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:no_destructor",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/hash",
        "//zetasql/base:check",
        "@com_google_absl//absl/log:die_if_null",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/random",
        "@com_google_absl//absl/random:distributions",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "//zetasql/base:source_location",
        "@com_google_absl//absl/types:span",
        "@com_google_differential_privacy//proto:confidence_interval_cc_proto",
        "@com_google_differential_privacy//proto:data_cc_proto",
        "@com_google_cc_differential_privacy//algorithms:algorithm",
        "@com_google_cc_differential_privacy//algorithms:bounded-sum",
        "@com_google_cc_differential_privacy//algorithms:bounded-mean",
        "@com_google_cc_differential_privacy//algorithms:bounded-standard-deviation",
        "@com_google_cc_differential_privacy//algorithms:bounded-variance",
        "@com_google_cc_differential_privacy//algorithms:quantiles",
        "@com_google_protobuf//:time_util",
        "//zetasql/base:flat_set",
        "//zetasql/base:map_util",
        "//zetasql/base:optional_ref",
        "//zetasql/base:stl_util",
        "//zetasql/base:exactfloat",
        "@com_googlesource_code_re2//:re2",
        "//zetasql/base:status",
        "@com_google_googleapis//google/rpc:status_cc_proto",
        "//zetasql/base:ret_check",
        "//zetasql/base:clock",
    ],
)

cc_library(
    name = "uda_evaluation",
    srcs = [
        "uda_evaluation.cc",
    ],
    hdrs = [
        "uda_evaluation.h",
    ],
    deps = [
        ":evaluation",
        ":type_helpers",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:builtin_function_cc_proto",
        "//zetasql/public:function",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/functions:differential_privacy_cc_proto",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/resolved_ast:serialization_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "tvf_evaluation",
    srcs = [
        "tvf_evaluation.cc",
    ],
    hdrs = [
        "tvf_evaluation.h",
    ],
    deps = [
        ":evaluation",
        ":type_helpers",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:builtin_function_cc_proto",
        "//zetasql/public:evaluator_table_iterator",
        "//zetasql/public:function",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/functions:differential_privacy_cc_proto",
        "//zetasql/public/types",
        "//zetasql/public/types:type",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/resolved_ast:serialization_cc_proto",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "function_evaluation_test",
    srcs = ["function_test.cc"],
    deps = [
        ":common",
        ":evaluation",
        "//zetasql/base:optional_ref",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common:evaluator_registration_utils",
        "//zetasql/common:internal_value",
        "//zetasql/public:interval_value",
        "//zetasql/public:json_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/types",
        "//zetasql/reference_impl/functions:graph",
        "//zetasql/reference_impl/functions:hash",
        "//zetasql/resolved_ast",
        "//zetasql/testdata:test_schema_cc_proto",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "tuple_test_util",
    testonly = 1,
    hdrs = ["tuple_test_util.h"],
    deps = [
        ":common",
        ":evaluation",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:internal_value",
        "//zetasql/public:value",
        "//zetasql/public/types:type",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest",
    ],
)

cc_library(
    name = "algebrizer",
    srcs = [
        "algebrizer.cc",
        "algebrizer_graph.cc",
    ],
    hdrs = [
        "algebrizer.h",
        "function.h",
        "operator.h",
        "tuple.h",
    ],
    deps = [
        ":common",
        ":evaluation",
        ":measure_evaluation",
        ":parameters",
        ":proto_util",
        ":tvf_evaluation",
        ":type_helpers",
        ":uda_evaluation",
        ":variable_generator",
        "//zetasql/analyzer:resolver",
        "//zetasql/base",
        "//zetasql/base:arena",
        "//zetasql/base:check",
        "//zetasql/base:flat_set",
        "//zetasql/base:map_util",
        "//zetasql/base:no_destructor",
        "//zetasql/base:optional_ref",
        "//zetasql/base:ret_check",
        "//zetasql/base:source_location",
        "//zetasql/base:status",
        "//zetasql/common:aggregate_null_handling",
        "//zetasql/common:internal_value",
        "//zetasql/common:measure_utils",
        "//zetasql/common:thread_stack",
        "//zetasql/public:anonymization_utils",
        "//zetasql/public:builtin_function_cc_proto",
        "//zetasql/public:catalog",
        "//zetasql/public:coercer",
        "//zetasql/public:collator_lite",
        "//zetasql/public:constant_evaluator",
        "//zetasql/public:evaluator_table_iterator",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:interval_value",
        "//zetasql/public:json_value",
        "//zetasql/public:language_options",
        "//zetasql/public:numeric_value",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:sql_constant",
        "//zetasql/public:sql_function",
        "//zetasql/public:sql_tvf",
        "//zetasql/public:templated_sql_function",
        "//zetasql/public:templated_sql_tvf_no_resolver",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/annotation:collation",
        "//zetasql/public/functions:array_zip_mode_cc_proto",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/functions:differential_privacy_cc_proto",
        "//zetasql/public/functions:json",
        "//zetasql/public/functions/match_recognize:compiled_pattern",
        "//zetasql/public/proto:type_annotation_cc_proto",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:column_factory",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/resolved_ast:rewrite_utils",
        "//zetasql/resolved_ast:serialization_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:no_destructor",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/functional:bind_front",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/synchronization",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest",
        "@com_google_protobuf//:protobuf",
        "@com_googlesource_code_re2//:re2",
    ],
)

cc_library(
    name = "parameters",
    hdrs = ["parameters.h"],
    deps = [":common"],
)

cc_library(
    name = "type_parameter_constraints",
    srcs = ["type_parameter_constraints.cc"],
    hdrs = ["type_parameter_constraints.h"],
    deps = [
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:internal_value",
        "//zetasql/public:numeric_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:timestamp_picos_value",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/functions:string",
        "//zetasql/public/types",
        "@com_google_absl//absl/numeric:int128",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "variable_generator",
    srcs = ["variable_generator.cc"],
    hdrs = ["variable_generator.h"],
    deps = [
        ":common",
        ":parameters",
        "//zetasql/base:status",
        "//zetasql/public:analyzer_options",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "reference_driver",
    testonly = 1,
    srcs = ["reference_driver.cc"],
    hdrs = ["reference_driver.h"],
    deps = [
        ":algebrizer",
        ":common",
        ":evaluation",
        ":parameters",
        ":rewrite_flags",
        ":statement_evaluator",
        ":type_helpers",
        ":variable_generator",
        "//zetasql/base",
        "//zetasql/base:check",
        "//zetasql/base:clock",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:evaluator_registration_utils",
        "//zetasql/common:status_payload_utils",
        "//zetasql/compliance:test_database_catalog",
        "//zetasql/compliance:test_driver",
        "//zetasql/proto:script_exception_cc_proto",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:catalog",
        "//zetasql/public:error_helpers",
        "//zetasql/public:evaluator",
        "//zetasql/public:function_headers",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:multi_catalog",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:parse_location",
        "//zetasql/public:prepared_expression_constant_evaluator",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:simple_catalog_util",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/annotation:collation",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/scripting:error_helpers",
        "//zetasql/scripting:script_executor",
        "//zetasql/scripting:script_segment",
        "//zetasql/scripting:type_aliases",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
    ],
)

# Link with this target for compliance tests against the reference
# implementation.  This returns a ReferenceDriver with no specific
# LanguageOptions set (because each test will specify the options to use
# for that test).
cc_library(
    name = "use_reference_driver_for_compliance_test",
    testonly = 1,
    srcs = ["use_reference_driver_for_compliance_test.cc"],
    deps = [
        ":reference_driver",
        ":rewrite_flags",
        "//zetasql/compliance:test_driver",
        "//zetasql/public:options_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "@com_google_absl//absl/flags:flag",
    ],
)

# A test of the reference implementation against the golden files, aliased here for completeness.
test_suite(
    name = "reference_compliance_goldens_test",
    tests = [
        "//zetasql/compliance:compliance_goldens_test",
    ],
)

# This test should pass 100% of compliance test queries for "kInternal" product mode ZetaSQL.
# This is the source of truth for (broken link).
zetasql_compliance_test(
    name = "reference_compliance_internal_test",
    size = "large",
    args = [
        # Do not turn on features 'in_development' for this target;
        # it affects compliance dashboard correctness.
        "--rewrites=minimal",
    ],
    known_error_files = [
        "//zetasql/compliance/known_errors:reference_impl_known_errors.textproto",
        "//zetasql/compliance/known_errors:zetasql_known_errors.textproto",
    ],
    shard_count = 15,
    deps = [
        ":reference_driver",
        # buildcleaner: keep
        ":use_reference_driver_for_compliance_test",
    ],
)

# This test should pass 100% of compliance test queries for "kExternal" product mode ZetaSQL.
# This is the source of truth for (broken link).
zetasql_compliance_test(
    name = "reference_compliance_external_test",
    size = "large",
    args = [
        # Do not turn on features 'in_development' for this target;
        # it affects compliance dashboard correctness.
        "--rewrites=minimal",
        "--zetasql_reference_driver_default_external_mode",
    ],
    known_error_files = [
        "//zetasql/compliance/known_errors:reference_impl_known_errors.textproto",
        "//zetasql/compliance/known_errors:zetasql_known_errors.textproto",
    ],
    shard_count = 15,
    deps = [
        ":reference_driver",
        # buildcleaner: keep
        ":use_reference_driver_for_compliance_test",
    ],
)

zetasql_compliance_test(
    name = "reference_compliance_test_all_rewrites",
    size = "large",
    args = [
        "--rewrites=all",
    ],
    known_error_files = [
        ":reference_impl_all_rewrites_known_errors.textproto",
        "//zetasql/compliance/known_errors:reference_impl_known_errors.textproto",
        "//zetasql/compliance/known_errors:zetasql_known_errors.textproto",
    ],
    shard_count = 30,
    deps = [
        ":reference_driver",
        # buildcleaner: keep
        ":use_reference_driver_for_compliance_test",
    ],
)

cc_test(
    name = "tuple_test",
    size = "small",
    srcs = ["tuple_test.cc"],
    deps = [
        ":common",
        ":evaluation",
        ":tuple_test_util",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common:internal_value",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/testdata:test_schema_cc_proto",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_library(
    name = "test_relational_op",
    testonly = 1,
    hdrs = ["test_relational_op.h"],
    deps = [
        ":common",
        ":evaluation",
        ":tuple_test_util",
        "//zetasql/base:ret_check",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "evaluation_context_test",
    size = "small",
    srcs = ["evaluation_context_test.cc"],
    deps = [
        ":evaluation",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "@com_google_absl//absl/time",
    ],
)

cc_test(
    name = "aggregate_op_test",
    size = "small",
    srcs = ["aggregate_op_test.cc"],
    deps = [
        ":common",
        ":evaluation",
        ":test_relational_op",
        ":tuple_test_util",
        "//zetasql/base",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:numeric_value",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/testdata:test_schema_cc_proto",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "analytic_op_test",
    size = "small",
    srcs = ["analytic_op_test.cc"],
    shard_count = 5,
    deps = [
        ":common",
        ":evaluation",
        ":test_relational_op",
        ":tuple_test_util",
        "//zetasql/base",
        "//zetasql/base:status",
        "//zetasql/base:stl_util",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:numeric_value",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/resolved_ast",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "pattern_matching_op_test",
    size = "small",
    srcs = ["pattern_matching_op_test.cc"],
    deps = [
        ":common",
        ":evaluation",
        ":test_relational_op",
        ":tuple_test_util",
        "//zetasql/base",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common/match_recognize:test_pattern_resolver",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/functions/match_recognize:compiled_pattern",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "relational_op_test",
    size = "large",
    srcs = ["relational_op_test.cc"],
    deps = [
        ":common",
        ":evaluation",
        ":test_relational_op",
        ":tuple_test_util",
        "//zetasql/base",
        "//zetasql/base:clock",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common:evaluator_test_table",
        "//zetasql/common:thread_stack",
        "//zetasql/public:evaluator_table_iterator",
        "//zetasql/public:function",
        "//zetasql/public:function_headers",
        "//zetasql/public:language_options",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/functions:array_zip_mode_cc_proto",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/testdata:test_schema_cc_proto",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "value_expr_test",
    size = "small",
    srcs = ["value_expr_test.cc"],
    shard_count = 5,
    deps = [
        ":common",
        ":evaluation",
        ":test_relational_op",
        ":tuple_test_util",
        ":type_helpers",
        ":variable_generator",
        "//zetasql/base",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common:graph_element_utils",
        "//zetasql/common:internal_value",
        "//zetasql/common:status_payload_utils",
        "//zetasql/compliance:functions_testlib",
        "//zetasql/public:builtin_function",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:catalog",
        "//zetasql/public:function",
        "//zetasql/public:id_string",
        "//zetasql/public:json_value",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/public/types:timestamp_util",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_ast_builder",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/testdata:test_schema_cc_proto",
        "//zetasql/testing:test_function",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/types:span",
        "@com_google_protobuf//:protobuf",
        "@com_google_protobuf//:protobuf_lite",
    ],
)

cc_test(
    name = "algebrizer_test",
    size = "small",
    srcs = ["algebrizer_test.cc"],
    deps = [
        ":algebrizer",
        ":common",
        ":parameters",
        ":variable_generator",
        "//zetasql/base:compact_reference_counted",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:builtin_function",
        "//zetasql/public:builtin_function_cc_proto",
        "//zetasql/public:catalog",
        "//zetasql/public:civil_time",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:make_node_vector",
        "//zetasql/resolved_ast:resolved_ast_builder",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:cord",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "parameters_test",
    size = "small",
    srcs = ["parameters_test.cc"],
    deps = [
        ":common",
        ":parameters",
        "//zetasql/base/testing:zetasql_gtest_main",
    ],
)

cc_test(
    name = "type_parameter_constraints_test",
    size = "small",
    srcs = ["type_parameter_constraints_test.cc"],
    deps = [
        ":type_parameter_constraints",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:numeric_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:type_parameters_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings:string_view",
    ],
)

cc_test(
    name = "variable_generator_test",
    size = "small",
    srcs = ["variable_generator_test.cc"],
    deps = [
        ":common",
        ":parameters",
        ":variable_generator",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:id_string",
        "//zetasql/public:type",
        "//zetasql/testing:test_value",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "rewrite_flags",
    srcs = ["rewrite_flags.cc"],
    hdrs = ["rewrite_flags.h"],
    deps = [
        "//zetasql/common:options_utils",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:options_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/strings",
    ],
)

proto_library(
    name = "evaluator_table_iterator_proto",
    srcs = ["evaluator_table_iterator.proto"],
)

cc_proto_library(
    name = "evaluator_table_iterator_cc_proto",
    deps = [":evaluator_table_iterator_proto"],
)

cc_library(
    name = "measure_evaluation",
    srcs = ["measure_evaluation.cc"],
    hdrs = ["measure_evaluation.h"],
    deps = [
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:catalog",
        "//zetasql/public:options_cc_proto",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "measure_evaluation_test",
    srcs = ["measure_evaluation_test.cc"],
    deps = [
        ":measure_evaluation",
        "//zetasql/base:check",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common:measure_analysis_utils",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:catalog",
        "//zetasql/public:id_string",
        "//zetasql/public:simple_catalog",
        "//zetasql/public/types",
        "//zetasql/public/types:type",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_ast_builder",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings:string_view",
    ],
)
